package com.zhongxi.guoyuan.view.user;

import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.application.MyApplication;
import com.zhongxi.guoyuan.base.BaseActivity;
import com.zhongxi.guoyuan.entity.BaseResponse;
import com.zhongxi.guoyuan.url.Url;

import java.util.HashMap;
import java.util.Map;

public class ModifyPasswordActivity extends BaseActivity implements View.OnClickListener {

    private EditText etOldPassword;
    private EditText etPassword;
    private TextView tvForgot;
    private TextView tvSure;
    private Map<String, String> map = new HashMap<>();
    private int position;

    @Override

    protected void initView() {
        super.initView();
        setContentView(R.layout.activity_modify_password);
        initTitle();
        tvTitle.setText("修改密码");
        etOldPassword = (EditText) findViewById(R.id.etOldPassword);
        etPassword = (EditText) findViewById(R.id.etPassword);
        tvForgot = (TextView) findViewById(R.id.tvForgot);
        tvSure = (TextView) findViewById(R.id.tvSure);

        position = getIntent().getIntExtra("position", 0);
        if(position == 0){
            tvForgot.setVisibility(View.VISIBLE);
        }else{
            tvForgot.setVisibility(View.GONE);
        }
    }

    @Override
    protected void initData() {
        super.initData();
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        ivBack.setOnClickListener(this);
        tvForgot.setOnClickListener(this);
        tvSure.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ivBack:
                finish();
                break;
            case R.id.tvForgot:
                startAct(ForgotPasswordActivity.class);
                break;
            case R.id.tvSure:
                setPassword();
                break;
        }
    }

    /**
     * 修改密码
     */
    private void setPassword() {
        String oldPassword = etOldPassword.getText().toString().trim();
        String newPassword = etPassword.getText().toString().trim();
        if(TextUtils.isEmpty(oldPassword)){
            showShortToast("原密码不能为空！");
        }else if(TextUtils.isEmpty(newPassword)){
            showShortToast("新密码不能为空！");
        }else{
            modifyPassword();
        }
    }

    private void modifyPassword() {
        map.clear();
        map.put("useid", MyApplication.getInstance().getUserId());
        map.put("newpwd", MyApplication.getInstance().descsc(etPassword.getText().toString().trim()));
        map.put("oldpwd",MyApplication.getInstance().descsc(etOldPassword.getText().toString().trim()));
        getServer(Url.SET_PWD, map,"", false, null, "post");
    }

    @Override
    protected void processSuccessResult(String response) {
        super.processSuccessResult(response);
        Gson gson = new Gson();
        try{
            BaseResponse result = gson.fromJson(response, BaseResponse.class);
            if(null!=result.getSafeobj()&&!"".equals(result.getSafeobj())){
                MyApplication.getInstance().setSpValue(result.getSafeobj());
            }
            showShortToast("修改密码成功！");
            finish();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    protected void ProcessFailResult(String response) {
        super.ProcessFailResult(response);
        Gson gson = new Gson();
        try{
            BaseResponse result = gson.fromJson(response, BaseResponse.class);
            if(null!=result.getSafeobj()&&!"".equals(result.getSafeobj())){
                MyApplication.getInstance().setSpValue(result.getSafeobj());
            }
            if(result.getCode()== 7003){
                showShortToast("原密码错误，请重试！！！");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
